package daily.jun0605;

import java.util.Arrays;

class Person{
    private String name;
    private int age;

    public int getAge() {
        return age;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class PersonLambdaTest {
    public static void main(String[] args) {
        Person[] people = {new Person("张三", 22),
                new Person("李四", 19),
                new Person("王五", 18),
                new Person("赵六", 21),
                new Person("田七", 20),
        };
        //使用匿名内部类重写Arrays类的sort方法
//       Arrays.sort(people, new Comparator<Person>() {
//           @Override
//           public int compare(Person o1, Person o2) {
//               return o1.getAge()-o2.getAge();//o1 - o2为升序
//           }
//       });
        //使用Lambda表达式重写Arrays类的sort方法
        Arrays.sort(people,(p1,p2)->p1.getAge()-p2.getAge());
        for (Person p : people) {
            System.out.println(p);
        }
    }
}
